package com.example.pet.service.impl;

import com.example.pet.beans.Pet;
import com.example.pet.common.Result;
import com.example.pet.mapper.IPetMapper;
import com.example.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName PetServiceImpl.java
 * @Description TODO
 * @createTime 2021年04月21日 00:43:00
 */
@Service
public class PetServiceImpl implements IPetService {

    private static final List ALLOW_IMAGE_TYPE = Arrays.asList("image/jpeg");

    @Autowired
    private IPetMapper iPetMapper;

    @Value("${file.img.location}")
    private String fileImgLocation;

    @Override
    public Result findAllPet() {
        List<Pet> petList = iPetMapper.findAllPet();
        return Result.succ(petList);
    }

    @Override
    public Result findAllPetByTypeId(String petTypeName) {
        return Result.succ(iPetMapper.findAllPetByTypeId(petTypeName));
    }

    @Override
    public Result addPet(Pet pet) {
        String petId = UUID.randomUUID().toString();
        pet.setPetId(petId)
                .setStop(1)
                .setStartTime(new Date());
        int i = iPetMapper.addPet(pet);
        if (i > 0) {
            return Result.succ(200, "新增成功", pet);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result updateByPetId(Pet pet) {
        int i = iPetMapper.updateByPetId(pet);
        if (i > 0) {
            return Result.succ(200, "修改成功", pet);
        }
        return Result.fail("修改失败");
    }

    /**
     * 根据分类获取所有宠物信息
     *
     * @param categoryId
     * @return
     */
    @Override
    public Result findPetByCategoryId(Integer categoryId) {
        List<Pet> petList = iPetMapper.findPetByCategoryId(categoryId);
        petList.forEach(System.out::println);
        return Result.succ(petList);
    }

    /**
     * 根据宠物编号修改宠物图片
     *
     * @param imgName
     * @param petId
     * @return
     */
    @Override
    public Result updatePetImgById(String imgName, String petId) {
        int i = iPetMapper.updatePetImgById(imgName, petId);
        if (i > 0) {
            return Result.succ("修改成功");
        }
        return Result.fail("修改失败");
    }

    /**
     * 根据宠物id获取宠物信息
     *
     * @param petId
     * @return
     */
    @Override
    public Result findPetByPetId(String petId) {
        Pet pet = iPetMapper.findPetByPetId(petId);
        return Result.succ(pet);
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @Override
    public Result uploadImg(MultipartFile file) {
        Result saveResult = saveImg(file);
        if (saveResult.getCode() == 200) {
            String imageName = saveResult.getData().toString();
            Map<String, Object> map = new HashMap<>();
            map.put("imgPath", imageName);
            return Result.succ("上传成功", map);
        }
        return Result.fail("上传失败");
    }

    private Result saveImg(MultipartFile file) {
        //判断图片类型
        String imageType = file.getContentType();
        if (!ALLOW_IMAGE_TYPE.contains(imageType)) {
            // 修改头像失败
            return Result.fail("上传失败");
        }
        //解析图片内容
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            // 修改头像失败
            return Result.fail("上传失败");
        }
        if (bufferedImage == null) {
            // 修改头像失败
            return Result.fail("上传失败");
        }
        //设置上传图片名称
        long time = new Date().getTime();
        String imageName = time + ".jpg";
        File imagePathFile = new File(fileImgLocation);
        //上传图片
        try {
            file.transferTo(new File(imagePathFile, imageName));
        } catch (IOException e) {
            // 修改头像失败
            return Result.fail("上传失败");
        }
        return Result.succ(imageName);
    }

}
